package com.code.ui;


import com.code.generator.DaoGenerator;
import com.code.generator.model.ColumnData;
import com.code.generator.model.GeneratorConfiguration;
import com.code.generator.model.TableInfo;
import com.code.util.StringUtils;
import java.awt.Component;
import java.awt.LayoutManager;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.ItemEvent;
import java.awt.event.ItemListener;
import java.util.List;
import javax.swing.DefaultCellEditor;
import javax.swing.DefaultComboBoxModel;
import javax.swing.JButton;
import javax.swing.JCheckBox;
import javax.swing.JComboBox;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JTable;
import javax.swing.table.AbstractTableModel;
import javax.swing.table.TableColumn;

public class TableGeneratorPanel extends JPanel {
    private static final long serialVersionUID = 1L;
    private final TableGeneratorPanel.MyAbstractTableModel tableModel;
    private final JTable table;
    private final JCheckBox generatorDomainCheckbox;
    private final JCheckBox generatorSqlMapCheckbox;
    private final JCheckBox generatorDaoCheckbox;
    private final JCheckBox generatorManagerCheckbox;
    private final JCheckBox generatorServiceCheckbox;
    private final JCheckBox generatorControllerCheckbox;
    private final JCheckBox needOverrideCheckBox;
    private final JCheckBox needShardingCheckBox;
    private final JCheckBox generateCreateCodeCheckBox;
    private final JCheckBox generateUpdateCodeCheckBox;
    private final JCheckBox generateQueryCodeCheckBox;
    private final JCheckBox generateDeleteCodeCheckBox;
    private final JComboBox tableComboBox;

    public TableGeneratorPanel(final GeneratorConfiguration generatorConfiguration, JFrame frame) {
        this.setLayout((LayoutManager)null);
        JLabel label_2 = new JLabel("第二步、选择表字段");
        label_2.setBounds(22, 10, 129, 15);
        this.add(label_2);
        
        JScrollPane scrollPane = new JScrollPane();
        scrollPane.setBounds(34, 88, 850, 286);
        this.add(scrollPane);
        
        TableInfo tableInfo = (TableInfo)generatorConfiguration.getSelectTableInfoList().get(0);
        this.tableModel = new TableGeneratorPanel.MyAbstractTableModel(tableInfo);
        this.table = new JTable(this.tableModel);
        this.table.putClientProperty("terminateEditOnFocusLost", Boolean.TRUE);
        scrollPane.setViewportView(this.table);
        this.table.setRowHeight(20);
        TableColumn tc3 = this.table.getColumnModel().getColumn(3);
        JCheckBox ckb = new JCheckBox();
        ckb.setSize(100, 26);
        tc3.setCellEditor(new DefaultCellEditor(ckb));
        
        JLabel label_3 = new JLabel("生成信息");
        label_3.setBounds(39, 405, 54, 23);
        this.add(label_3);
        this.generatorDomainCheckbox = new JCheckBox("生成Domain");
        this.generatorDomainCheckbox.setBounds(99, 405, 103, 23);
        this.add(this.generatorDomainCheckbox);
        this.generatorSqlMapCheckbox = new JCheckBox("生成SqlMap");
        this.generatorSqlMapCheckbox.setBounds(204, 405, 103, 23);
        this.add(this.generatorSqlMapCheckbox);
        this.generatorDaoCheckbox = new JCheckBox("生成DAO");
        this.generatorDaoCheckbox.setBounds(325, 405, 93, 23);
        this.add(this.generatorDaoCheckbox);
        this.generatorManagerCheckbox = new JCheckBox("生成Manager");
        this.generatorManagerCheckbox.setBounds(425, 405, 103, 23);
        this.add(this.generatorManagerCheckbox);
        this.generatorServiceCheckbox = new JCheckBox("生成Service");
        this.generatorServiceCheckbox.setBounds(551, 405, 103, 23);
        this.add(this.generatorServiceCheckbox);
        this.generatorControllerCheckbox = new JCheckBox("生成Controller");
        this.generatorControllerCheckbox.setBounds(656, 405, 129, 23);
        this.add(this.generatorControllerCheckbox);
        this.needOverrideCheckBox = new JCheckBox("当存在时是否覆盖");
        this.needOverrideCheckBox.setBounds(656, 446, 182, 23);
        this.add(this.needOverrideCheckBox);
        this.needShardingCheckBox = new JCheckBox("分库分表");
        this.needShardingCheckBox.setBounds(551, 446, 103, 23);
        this.add(this.needShardingCheckBox);
        this.generateCreateCodeCheckBox = new JCheckBox("生成新增代码");
        this.generateCreateCodeCheckBox.setBounds(99, 446, 103, 23);
        this.add(this.generateCreateCodeCheckBox);
        this.generateUpdateCodeCheckBox = new JCheckBox("生成修改代码");
        this.generateUpdateCodeCheckBox.setBounds(204, 446, 103, 23);
        this.add(this.generateUpdateCodeCheckBox);
        this.generateQueryCodeCheckBox = new JCheckBox("生成查询代码");
        this.generateQueryCodeCheckBox.setBounds(425, 446, 103, 23);
        this.add(this.generateQueryCodeCheckBox);
        this.generateDeleteCodeCheckBox = new JCheckBox("生成删除代码");
        this.generateDeleteCodeCheckBox.setBounds(325, 446, 103, 23);
        this.add(this.generateDeleteCodeCheckBox);
        
        final JButton generatorbutton = new JButton("生成");
        generatorbutton.setBounds(216, 492, 93, 23);
        this.add(generatorbutton);
        generatorbutton.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent event) {
                generatorbutton.setEnabled(false);

                try {
                    TableInfo e1 = generatorConfiguration.getSelectTableInfo(TableGeneratorPanel.this.tableComboBox.getSelectedItem().toString());
                    TableGeneratorPanel.this.buildGeneratorConfiguration(generatorConfiguration, e1);
                    (new DaoGenerator()).generate(generatorConfiguration);
                    JOptionPane.showMessageDialog((Component)null, "生成成功了！");
                } catch (Exception var3) {
                    JOptionPane.showMessageDialog((Component)null, "生成失败了：" + var3.getMessage());
                }

                generatorbutton.setEnabled(true);
            }
        });
        this.initComponent(tableInfo);
        JLabel label = new JLabel("选择表");
        label.setBounds(34, 50, 77, 15);
        this.add(label);
        this.tableComboBox = new JComboBox();
        this.tableComboBox.addItemListener(new ItemListener() {
            public void itemStateChanged(ItemEvent e) {
                TableInfo tableInfo;
                if(e.getStateChange() == ItemEvent.SELECTED) {
                    tableInfo = generatorConfiguration.getSelectTableInfo(TableGeneratorPanel.this.tableComboBox.getSelectedItem().toString());
                    TableGeneratorPanel.this.initComponent(tableInfo);
                    TableGeneratorPanel.this.tableModel.setData(TableGeneratorPanel.this.initTable(tableInfo));
                    TableGeneratorPanel.this.table.repaint();
                } else if(e.getStateChange() == ItemEvent.DESELECTED) {
                    tableInfo = generatorConfiguration.getSelectTableInfo(e.getItem().toString());
                    TableGeneratorPanel.this.buildGeneratorConfiguration(generatorConfiguration, tableInfo);
                }

            }
        });
        this.tableComboBox.setBounds(98, 47, 210, 21);
        this.add(this.tableComboBox);
        this.tableComboBox.setModel(new DefaultComboBoxModel(StringUtils.split(generatorConfiguration.getSelectTables(), generatorConfiguration.getSplitChar())));
        
        JButton exitButton = new JButton("退出");
        exitButton.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent e) {
                System.exit(0);
            }
        });
        exitButton.setBounds(551, 492, 93, 23);
        this.add(exitButton);
    }

    private void buildGeneratorConfiguration(GeneratorConfiguration generatorConfiguration, TableInfo tableInfo) {
        tableInfo.setGenerateDomain(this.generatorDomainCheckbox.isSelected());
        tableInfo.setGenerateSqlMap(this.generatorSqlMapCheckbox.isSelected());
        tableInfo.setGenerateDao(this.generatorDaoCheckbox.isSelected());
        tableInfo.setGenerateManager(this.generatorManagerCheckbox.isSelected());
        tableInfo.setGenerateService(this.generatorServiceCheckbox.isSelected());
        tableInfo.setGenerateController(this.generatorControllerCheckbox.isSelected());
        tableInfo.setGenerateCreate(this.generateCreateCodeCheckBox.isSelected());
        tableInfo.setGenerateUpdate(this.generateUpdateCodeCheckBox.isSelected());
        tableInfo.setGenerateDelete(this.generateDeleteCodeCheckBox.isSelected());
        tableInfo.setGenerateQuery(this.generateQueryCodeCheckBox.isSelected());
        tableInfo.setNeedSharding(this.needShardingCheckBox.isSelected());
        tableInfo.setOnExistsOverride(this.needOverrideCheckBox.isSelected());
        List columnDataList = tableInfo.getColumnDataList();

        for(int i = 0; i < this.tableModel.getRowCount(); ++i) {
            ColumnData columnData = (ColumnData)columnDataList.get(i);
            columnData.setGenerateDomain(((Boolean)this.tableModel.getValueAt(i, 4)).booleanValue());
            columnData.setGeneratePage(((Boolean)this.tableModel.getValueAt(i, 5)).booleanValue());
            columnData.setNeedSum(((Boolean)this.tableModel.getValueAt(i, 6)).booleanValue());
        }

    }

    private void initComponent(TableInfo tableInfo) {
        if(tableInfo != null) {
            this.generatorDomainCheckbox.setSelected(tableInfo.isGenerateDomain());
            this.generatorSqlMapCheckbox.setSelected(tableInfo.isGenerateSqlMap());
            this.generatorDaoCheckbox.setSelected(tableInfo.isGenerateDao());
            this.generatorManagerCheckbox.setSelected(tableInfo.isGenerateManager());
            this.generatorServiceCheckbox.setSelected(tableInfo.isGenerateService());
            this.generatorControllerCheckbox.setSelected(tableInfo.isGenerateController());
            this.generateCreateCodeCheckBox.setSelected(tableInfo.isGenerateCreate());
            this.generateUpdateCodeCheckBox.setSelected(tableInfo.isGenerateUpdate());
            this.generateDeleteCodeCheckBox.setSelected(tableInfo.isGenerateDelete());
            this.generateQueryCodeCheckBox.setSelected(tableInfo.isGenerateQuery());
            this.needShardingCheckBox.setSelected(tableInfo.isNeedSharding());
            this.needOverrideCheckBox.setSelected(tableInfo.isOnExistsOverride());
        }

    }

    private Object[][] initTable(TableInfo tableInfo) {
        List columnDataList = tableInfo.getColumnDataList();
        Object[][] tableData = new Object[columnDataList.size()][];

        for(int i = 0; i < tableData.length; ++i) {
            ColumnData columnData = (ColumnData)columnDataList.get(i);
            tableData[i] = new Object[]{columnData.getColumnName(), columnData.getColumnComment(), columnData.getColumnType(), Boolean.valueOf(columnData.isKeyColumn()), Boolean.valueOf(columnData.isGenerateDomain()), Boolean.valueOf(columnData.isGeneratePage()), Boolean.valueOf(columnData.isNeedSum())};
        }

        return tableData;
    }

    class MyAbstractTableModel extends AbstractTableModel {
        String[] head = new String[]{"字段名", "字段描述", "字段类型", "是否主键", "生成实体", "生成页面", "生成汇总"};
        Class[] typeArray = new Class[]{String.class, String.class, String.class, Boolean.class, Boolean.class, Boolean.class, Boolean.class};
        private Object[][] data = null;

        public MyAbstractTableModel(TableInfo tableInfo) {
            this.data = TableGeneratorPanel.this.initTable(tableInfo);
        }

        public int getColumnCount() {
            return this.head.length;
        }

        public int getRowCount() {
            return this.data.length;
        }

        public String getColumnName(int column) {
            return this.head[column];
        }

        public Object getValueAt(int rowIndex, int columnIndex) {
            return this.data[rowIndex][columnIndex];
        }

        public boolean isCellEditable(int rowIndex, int columnIndex) {
            return columnIndex >= 4;
        }

        public void setValueAt(Object aValue, int rowIndex, int columnIndex) {
            this.data[rowIndex][columnIndex] = aValue;
            this.fireTableCellUpdated(rowIndex, columnIndex);
        }

        public Class getColumnClass(int columnIndex) {
            return this.typeArray[columnIndex];
        }

        public Object[][] getData() {
            return this.data;
        }

        public void setData(Object[][] data) {
            this.data = data;
        }
    }
}

